package client_controllers;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.IOException;
import java.util.ArrayList;

import client_gui.SalesManGUI;
import client_logic.CcrmClient;
import client_logic.dbHandler;
import entity.AddressingCustomer;
import entity.Boss;
import entity.CampaignPattern;
import entity.Customer;
import entity.Employee;

public class SalesManController implements dbHandler{
	/**
	 * @class SalesManController allows communications between marketing employees and DB to extract customer information
	 * 
	 * @param SalesManGUI links to GUI.
	 * @param EmployeeLoginController links to check fields for employee authorization. 
	 * @param Boss is used as an instance, to send objects to DB and allow changes in the lists.
	 * @param AddressingCustomer is for a new AddressingCustomer instance.
	 */
	
	private SalesManGUI smg ;
	private EmployeeLoginController elc;
	private Boss b;
	private AddressingCustomer ac;

public SalesManController(SalesManGUI smg,EmployeeLoginController elc){
	this.smg=smg;
	this.elc=elc;
	this.getSmg().addWindowListener(new Exit());
	this.getSmg().getBtnGetCampaigns().addActionListener(new GetCamp());
	this.getSmg().getBtnLogout().addActionListener(new Logout());
	this.getSmg().getBtnYes().addActionListener(new YesBtn());
	this.getSmg().getBtnNo().addActionListener(new NoBtn());
	this.getSmg().getBtnAddPermition().addActionListener(new AddPermitionLis());
	this.getSmg().getBtnGetProduct().addActionListener(new GetProductLis());
	this.getSmg().getBtnFinish().addActionListener(new FinishLis());
	getCustomer();
}


@Override
public void Add() {
	// TODO Auto-generated method stub
	
}

@Override
public void Search() {
	try {
		CcrmClient.inet.setCurrCon(this);
		CcrmClient.inet.sendToServer(b);
	} catch (IOException e) {
		e.printStackTrace();
	}
	
}

@Override
public String Delete() {
	// TODO Auto-generated method stub
	return null;
}

@Override
public void HandleMsg(Object msg) {
	// TODO Auto-generated method stub
	this.b=(Boss) msg;
	 if(this.getB().getCommand().equals("getCustomer")){
			//this.getCsg().dispose();
			//this.getElc().getElg().setVisible(true);
			ArrayList<Integer>  customer ;
			customer= (ArrayList<Integer>) this.getB().getObj();
			if(customer.size()==0){
				smg.getLblError_1().setText("NO Customers!");
			    smg.getLblError_1().setVisible(true);
			    
			}
			else{
				for(int i :customer)
				smg.getCustomerBox().addItem(i);
				smg.getBtnGetCampaigns().setVisible(true);
				}
	 		}
	 
	 else if(this.getB().getCommand().equals("getCampaign")){
			ArrayList<Integer>  camp ;
			camp= (ArrayList<Integer>) this.getB().getObj();
			for(int i :camp)
				smg.getCampaignBox().addItem(i);
	 }
	 else if(this.getB().getMsg().equals("logout")){
			this.getSmg().dispose();
			this.getElc().getElg().setVisible(true);
	}
		
	else if(this.getB().getCommand().equals("get saled product")){
		ArrayList<Object>  details ;
		details= (ArrayList<Object>) this.getB().getObj();
		for(Object i :details){
			smg.getLblproduct().setText((String) i);
			ac.setItem((String) i);
		}
	}
	else if(this.getB().getCommand().equals("getFuturePermission")){
			ArrayList<String>  category ;
			category= (ArrayList<String>) this.getB().getObj();
			for(String i :category)
				smg.getFuturePermissionCombo().addItem(i);
			getSmg().getPanelFP().setVisible(true);	
		}
	else if(this.getB().getCommand().equals("New Sale or Deny")){
		smg.dispose();
		smg=new SalesManGUI(elc);
	}
	 
	}

@Override
public void update() {
	try {
		CcrmClient.inet.setCurrCon(this);
		CcrmClient.inet.sendToServer(b);
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	
}

public void create_boss(){
	this.setB(new Boss(null,"add","SalesManController"));
}

public class Logout implements ActionListener {
    public void actionPerformed(ActionEvent ev){
    		setB(new Boss(getElc().getEmp(),"logout","SalesManController"));
    		update();
    	}
}

public class Exit implements WindowListener{

	@Override
	public void windowActivated(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowClosed(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowClosing(WindowEvent e) {
		// TODO Auto-generated method stub
		setB(new Boss(getElc().getEmp(),"logout","SalesManController"));
		update();
		
		
	}

	@Override
	public void windowDeactivated(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowDeiconified(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowIconified(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowOpened(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}
	
}

public SalesManGUI getSmg() {
	return smg;
}

public void setSmg(SalesManGUI smg) {
	this.smg = smg;
}

public EmployeeLoginController getElc() {
	return elc;
}

public void setElc(EmployeeLoginController elc) {
	this.elc = elc;
}

public Boss getB() {
	return b;
}

public void setB(Boss b) {
	this.b = b;
}
private void getCustomer() {

	Employee emp = getElc().getEmp();
	this.setB(new Boss(emp,"getCustomer","SalesManController"));
	this.Search();	
}
public class GetCamp implements ActionListener{
	public void actionPerformed(ActionEvent e) {
		getSmg().getLblChooseCampaign().setVisible(true);	
		getSmg().getBtnGetProduct().setVisible(true);
	//	getSmg().getPhonetext().setVisible(true);
	//	getSmg().getLblPhone().setVisible(true);
		//getSmg().getLblsuccess().setVisible(true);
		//getSmg().getBtnNo().setVisible(true);
		//getSmg().getBtnYes().setVisible(true);
		getSmg().getCampaignBox().setVisible(true);
		ac = new AddressingCustomer();
		ac.setcId((int) getSmg().getCustomerBox().getSelectedItem());
		ac.seteId(elc.getEmp().getId());
		ac.setCategories_permit(new ArrayList<String>());
		b.setObj(ac);
		b.setCommand("getCampaign");
		b.setControl("SalesManController");
		Search();
	}	
}
public class YesBtn implements ActionListener{
	public void actionPerformed(ActionEvent e) {
		getSmg().getSuccess().setVisible(true);
		getSmg().getBtnNo().setVisible(false);
		ac.setSaleFlag(true);
		
		getFuturePermis();
	}
}

public class NoBtn implements ActionListener{
	public void actionPerformed(ActionEvent e) {
		getSmg().getFail().setVisible(true);
		getSmg().getBtnYes().setVisible(false);
		getSmg().getPanelFP().setVisible(true);
		ac.setSaleFlag(false);
		getFuturePermis();
		//ac.setFreeText(smg.getTextFieldCustomerComment().getText());
		//b.setCommand("get saled product");
		//b.setObj(ac);
		//b.setControl("SalesManController");
		//Search();
	}
}

private void getFuturePermis() {
	
	this.setB(new Boss(ac,"getFuturePermission","SalesManController"));
	this.Search();	
}

public class AddPermitionLis implements ActionListener{
	public void actionPerformed(ActionEvent e) {
		ac.getCategories_permit().add((String) smg.getFuturePermissionCombo().getSelectedItem().toString());
		//setB(new Boss(ac,"addpermission","SalesManController"));
		//Add();
		smg.getFuturePermissionCombo().removeItem(smg.getFuturePermissionCombo().getSelectedItem());		
	}
	
}

public class FinishLis implements ActionListener{
	public void actionPerformed(ActionEvent e) {
		ac.setPhoneCall(Double.parseDouble(smg.getPhonetext().getText()));
		if(ac.getSaleFlag()){
			ac.setQuntity(Integer.parseInt(smg.getTextFieldQuantity().getText()));
	
		}
		else{
			ac.setFreeText(smg.getFreeText().getText());
			
		}
		b.setObj(ac);
		b.setControl("SalesManController");
		b.setCommand("New Sale or Deny");
		update();
		
		
	}
	
}

public class GetProductLis implements ActionListener{
	public void actionPerformed(ActionEvent e) {
		ac.setCpId((int) getSmg().getCampaignBox().getSelectedItem());
		b.setCommand("get saled product");
		b.setObj(ac);
		b.setControl("SalesManController");
		Search();
		smg.getLblproduct().setVisible(true);
		smg.getLblItem().setVisible(true);
		smg.getLblsuccess().setVisible(true);
		smg.getBtnYes().setVisible(true);
		smg.getBtnNo().setVisible(true);
		smg.getLblPhone().setVisible(true);
		smg.getPhonetext().setVisible(true);
		
	}
	
}


}
